﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Innova.Microfinanzas.BusinessEntities;
using Innova.Microfinanzas.BusinessEntities.SystemEnums;

namespace MyCommerce.Business.GeneralParameters
{
    public class CompositionOfCodeManager
    {
        private MyCommerceEntities vContext;
        private static int codeLength = 5;

        public CompositionOfCodeManager()
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = false;
            }
        }

        public CompositionOfCodeManager(bool lazyLoad)
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = lazyLoad;
            }
        }

        public string GenerateCode(CompositionOfCodeTypes codeType, long idStore)
        {
            string codeGenerated = "";
            string codeStore = "";
            string codeSequence = "";
            int sequential = 1;
            string separator = "";
            long idCode = Convert.ToInt64(codeType);
                        
            if (codeType == CompositionOfCodeTypes.OrderToSupplierCode)
            {
                var objStore = (from cc in vContext.Store
                                where cc.IdStore == idStore
                                select cc).FirstOrDefault();
                if (objStore == null)
                    throw new Exception("El usuario no tiene un almacén asignado.");
                codeStore = objStore.Code;
            }
            else
            {
                var objStore = (from cc in vContext.DealerStore
                            where cc.IdDealerStore == idStore
                                select cc).FirstOrDefault();
                if (objStore == null)
                    throw new Exception("El usuario no tiene una tienda asignada.");
                idStore = objStore.IdStore;
                codeStore = objStore.Code;
            }

            var result = (from cc in vContext.CompositionOfCodeSequence
                                   where cc.IdCompositionOfCode == idCode
                                   && cc.IdStore == idStore
                                   select cc).FirstOrDefault();

            var codeFather = (from cc in vContext.CompositionOfCode
                          where cc.IdCompositionOfCode == idCode
                          select cc).First();
            if (result == null)
                result = new CompositionOfCodeSequence();

            if (result != null
                && codeFather != null)
            {
                separator = codeFather.Separator;
                if (result.IdCompositionOfCodeSequence != 0)
                {
                    sequential = result.Sequential;
                    result.Sequential = sequential + 1;
                }
                else
                {
                    result.IdCompositionOfCode = codeFather.IdCompositionOfCode;
                    result.IdStore = idStore;
                    result.Sequential = sequential + 1;
                    vContext.AddObject("CompositionOfCodeSequence", result);
                }
            }

            codeSequence = sequential.ToString();
            int index = codeLength - codeSequence.Length;

            for (int indexChar = 0; indexChar < index; indexChar++)
            {
                codeSequence = codeSequence.Insert(0, "0");
            }

            codeGenerated = codeStore + separator + codeSequence;
            vContext.SaveChanges();
            return codeGenerated;
        }
    }
}
